﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity.Validation;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using IBatisNet.DataMapper;
using Kaiser.Modules.Hiner.Models.Order;
using Kaiser.Modules.Hiner.Models.Product.SearchModels;
using Kaiser.Modules.Hiner.Services;
using Kaiser.Modules.Hiner.Models.Order.SearchModels;
using Kaiser.Models;
using Kaiser.Modules.Hiner.Models;
using Kaiser.Extensions;
using System.Web.Script.Serialization;
using Kaiser.Models.JsonModels;
using Kaiser.Infrastructure.Controllers;
using Kaiser.Modules.Hiner.Models.Product;

namespace Kaiser.Modules.Hiner.Controllers
{
    public class OrderManageController : KaiserBaseController
    {


        #region 变量和属性
        private readonly IOrderService _orderService;

        public OrderManageController(IOrderService orderService, IProductService productService)
        {
            this._orderService = orderService;
            _productService = productService;
        }

        private readonly IProductService _productService;
        #endregion

        public ActionResult OrderListYqr()
        {
            return View();
        }

        [HttpPost]
        public ActionResult OrderListYqr(OrderSearch search, PagingInfo pageinfo, FormCollection form)
        {
            search.OrderStatus = "1";
            var result = _orderService.SearchOrder(search, pageinfo);
            return Json(result);
        }

        public ActionResult OrderListYjf()
        {
            return View();
        }

        [HttpPost]
        public ActionResult OrderListYjf(OrderSearch search, PagingInfo pageinfo, FormCollection form)
        {
            search.OrderStatus = "2";
            var result = _orderService.SearchOrder(search, pageinfo);
            return Json(result);
        }

        public ActionResult OrderList()
        {
            return View();
        }


        /// <summary>
        /// 订单列表
        /// </summary>
        /// <param name="search"></param>
        /// <param name="pageinfo"></param>
        /// <param name="form"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult OrderList(OrderSearch search, PagingInfo pageinfo, FormCollection form)
        {
            var result = _orderService.SearchOrder(search, pageinfo);


            return Json(result);
        }

        /// <summary>
        /// 新增订单
        /// </summary>
        /// <param name="model"></param>
        /// <param name="form"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult AddOrder(OrderBase model)
        {
            var result = new JsonReturnMessages();

            try
            {
                model.CreateUserFullName = User.Identity.Name;
                model.CreateUserName = CurrentUser.Name;
                model.CreateDateTime = DateTime.Now;
                _orderService.Insert(model);

                result.success = true;
                result.msg = "保存成功！";
            }

            catch (Exception ex)
            {
                result.success = false;
                result.msg = ex.Message;
            }
            return Json(result);

        }


        public ActionResult GetProductList(ProductSearch search, PagingInfo pageinfo)
        {
            var result = _productService.SearchProduct(search, pageinfo).rows;
            return Json(result);
        }

        public ActionResult AddOrder()
        {
            var model = new OrderBase();
            model.OrderId = Guid.NewGuid().GenerateGuid().ToString();

            return View("EditOrder", model);
        }

        public ActionResult ViewOrder(string id)
        {
            var model = _orderService.Get(id);
            return View(model);
        }

        public ActionResult EditOrder(string id)
        {
            var model = _orderService.Get(id);
            return View(model);
        }

        /// <summary>
        /// 编辑订单
        /// </summary>
        /// <param name="id"></param>
        /// <param name="model"></param>
        /// <param name="form"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult EditOrder(string id, OrderBase model)
        {
            var result = new JsonReturnMessages();

            try
            {

                if (id.IsNullOrWhiteSpace())
                {
                    model.OrderId = Guid.NewGuid().GenerateGuid().ToString();
                    model.CreateUserFullName = User.Identity.Name;
                    model.CreateUserName = CurrentUser.Name;
                    model.CreateDateTime = DateTime.Now;
                    _orderService.Insert(model);
                }
                else
                {
                    if (ModelState.IsValid)
                    {
                        OrderBase oldSort = _orderService.Get(id);
                        UpdateModel(oldSort, Request.Form.AllKeys);
                        //UpdateModel(oldSort, "OrderDetailList", Request.Form.AllKeys);
                        oldSort.OrderDetailList = model.OrderDetailList;
                        oldSort.LastModifyDateTime = DateTime.Now;
                        oldSort.LastModifyUserFullName = User.Identity.Name;
                        oldSort.LastModifyUserName = CurrentUser.Name;
                        oldSort.OrderStatus = 0;
                        _orderService.Update(oldSort);
                    }
                    else
                    {
                        result.success = false;
                        result.msg = "保存验证失败！";
                    }
                }
                result.success = true;
                result.msg = "保存成功！";
            }
            catch (DbEntityValidationException ex)
            {
                result.success = false;
                result.msg = ex.Message;
            }
            catch (Exception ex)
            {
                result.success = false;
                result.msg = ex.Message;
            }
            return Json(result);
        }

        /// <summary>
        /// 批量删除订单
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public ActionResult DelOrder(string ids)
        {
            var result = new JsonReturnMessages(); ;
            if (!ids.IsNullOrWhiteSpace())
            {
                try
                {
                    for (int i = 0; i < ids.Split(',').Length; i++)
                    {
                        _orderService.Delete(ids.Split(',')[i]);
                    }

                    result.success = true;
                    result.msg = "删除成功";
                }
                catch (Exception)
                {
                    result.success = false;
                    result.msg = "删除失败";

                }
            }
            else
            {
                result.msg = "传值失败，请告诉开发者解决";
            }
            var json = new JavaScriptSerializer();
            return Content(json.Serialize(result), "text/html;charset=UTF-8");
        }

        public ActionResult OrderConfirm(string orderId)
        {
            var result = new JsonReturnMessages();

            try
            {
                _orderService.OrderConfirm(orderId);
                result.success = true;
            }

            catch (Exception ex)
            {
                result.success = false;
                result.msg = ex.Message;
            }
            return Json(result);
        }

        public ActionResult OrderDelivery(string orderId)
        {
            var result = new JsonReturnMessages();

            try
            {
                _orderService.OrderDelivery(orderId);
                result.success = true;
            }

            catch (Exception ex)
            {
                result.success = false;
                result.msg = ex.Message;
            }
            return Json(result);
        }
    }
}
